// pages/edit_onoff_template/edit_onoff_template.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    template_name: '',
    index_on: 0,
    index_off: 0,
    poweron_time: '00:01',
    poweroff_time: '23:59',
    on_adjust_num: 0,
    off_adjust_num: 0,
    isdisabled_on: false, //disabled 表示禁用  true为禁用 false为不禁用
    isdisabled_off: false,
    time_type_array: [
      {
        id: "0",
        name: "指定时间"
      },
      {
        id: "1",
        name: "据设备经纬度自动计算"
      }
    ] 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      // {
    // id: "2"
    // name: "测试1"
    // turnOff: "16:00:00"
    // turnOffChange: "20"
    // turnOn: "auto"
    // turnOnChange: "-10" 
    // }
      id: options.id,
      template_name: options.name,
      // turnOff: options.turnOff,
      // turnOffChange: options.turnOffChange,
      // turnOn: options.turnOn,
      // turnOnChange: options.turnOnChange,

      index_on: options.turnOn=="auto"?1:0,
      index_off: options.turnOff=="auto"?1:0,
      poweron_time: options.turnOn == "auto" ? '00:01' : options.turnOn,
      poweroff_time: options.turnOff == "auto" ? '23:59' : options.turnOff ,
      on_adjust_num: options.turnOnChange,
      off_adjust_num: options.turnOffChange,
      isdisabled_on: options.turnOn == "auto"?true:false, //disabled 表示禁用  true为禁用 false为不禁用
      isdisabled_off: options.turnOff == "auto"?true:false    
    });    
  },

  formSubmit: function (e) {
    let that = this;
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
      'cookie': "openid=" + app.globalData.openid
    }

    if (e.detail.value.input_templatename == "") {
      wx.showModal({
        title: '提示',
        content: '单灯名称不能为空',
        showCancel: false
      });

      return;
    }

    /**
     *  index_on:0,
    index_off:0,
    poweron_time: '00:00',
    poweroff_time: '23:59',
    on_adjust_num:0,
    off_adjust_num: 0,
    isdisabled_on:true,
    isdisabled_off: true,
    */

    that.setData({
      template_name: e.detail.value.input_templatename,
      index_off: e.detail.value.off_picker_type,
      index_on: e.detail.value.on_picker_type,
      poweron_time: e.detail.value.picker_poweron_time,
      poweroff_time: e.detail.value.picker_poweroff_time,
      on_adjust_num: e.detail.value.on_adjust,
      off_adjust_num: e.detail.value.off_adjust,
      isdisabled_on: e.detail.value.on_picker_type ? false : true,
      isdisabled_off: e.detail.value.off_picker_type ? false : true
    });

    //let tmp_lamp_type = that.data.lamp_type_array[that.data.index].name;

    wx.request({
      url: 'https://119.3.46.145/server/UpdateLightModal',
      method: 'Post',
      data: {
        modalName: that.data.template_name,
        turnOnType: that.data.index_on,
        turnOffType: that.data.index_off,
        modalTurnOn: that.data.poweron_time,
        modalTurnOff: that.data.poweroff_time,
        turnOnChange: that.data.on_adjust_num,
        turnOffChange: that.data.off_adjust_num,
        userid: app.globalData.userid,
        id: that.data.id
      },
     
      header: header,
      success: res => {
        //data: { error: "0", info: "添加成功" }  {error: "-1", info: "已存在，无法添加"}        
        let result = res.data;

        if (result.error == "0") {
          wx.showModal({
            title: '提示',
            content: '修改成功',
            success: function () {
              wx.request({
                url: 'https://119.3.46.145/server/GetLightModal',
                header: header,
                data: {
                  userid: app.globalData.userid
                },
                success: function (res) {
                  // { "data": "[{\"turnOff\":\"16:00:00\",\"turnOffChange\":\"20\",\"turnOn\":\"auto\",\"name\":\"测试1\",\"id\":\"2\",\"turnOnChange\":\"-10\"},{\"turnOff\":\"auto\",\"turnOffChange\":\"0\",\"turnOn\":\"auto\",\"name\":\"测试3\",\"id\":\"4\",\"turnOnChange\":\"0\"},{\"turnOff\":\"07:30:00\",\"turnOffChange\":\"10\",\"turnOn\":\"06:03:00\",\"name\":\"测试2-1\",\"id\":\"5\",\"turnOnChange\":\"-10\"},{\"turnOff\":\"04:59\",\"turnOffChange\":\"10\",\"turnOn\":\"18:01\",\"name\":\"test5\",\"id\":\"7\",\"turnOnChange\":\"10\"},{\"turnOff\":\"23:59\",\"turnOffChange\":\"10\",\"turnOn\":\"00:01\",\"name\":\"test6\",\"id\":\"8\",\"turnOnChange\":\"10\"}]" }
                  let data = JSON.parse(res.data.data);

                  if (data.length == 0) {
                    return;
                  }

                  let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
                  let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
                  prevPage.setData({
                    template_array: data
                  });
                  wx.navigateBack(); //关闭当前页面，返回上一个页面
                },
                fail: function (error) {
                  console.log(error, "GetLightModal fail");
                }
              });
            }
          })

        }
        else {
          wx.showModal({
            title: '提示',
            content: '修改失败',
            showCancel: false
          })
        }

        // let error = JSON.parse(response).error;
        // if (error === "true") {
        //   dismissAdd();
        //   alert("添加成功");

        //   refreshModalTable();
        // }
        // else {
        //   alert("添加失败");
        // }
      },
      fail: function (error) {
        console.log(error, "addLightModal error");
      }
    })
  },

  //开灯时间类型类型
  onbindPickerChange: function (e) {
    let that = this;
    let value = parseInt(e.detail.value);
    that.setData({
      index_on: value,
      isdisabled_on: value ? true : false
    });

    if (value == 1) {
      that.setData({
        poweron_time: "auto"
      });
    }


  },

  //开灯时间
  bindpoweronChange: function (e) {
    let that = this;
    that.setData({
      poweron_time: e.detail.value
    });
  },

  //关灯时间类型类型
  offbindPickerChange: function (e) {
    let that = this;
    let value = parseInt(e.detail.value);
    that.setData({
      index_off: value,
      isdisabled_off: value ? true : false
    });

    if (value == 1) {
      that.setData({
        poweroff_time: "auto"
      });
    }
  },

  //关灯时间
  bindpoweroffChange: function (e) {
    let that = this;
    that.setData({
      poweroff_time: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})